Python 3.6 for beginners
I strongly recommend installing Anaconda (Python distribution). It comes with numpy and scipy preinstalled. Syntax There is one thing that every beginner needs to understand from the very start. When you tell the computer something like :Pront("I want the computer to print this to the screen") it isnt going to understand you. No matter how obvious it is to you, and indeed every living thing on the face of the planet, it just isnt going to understand you. No matter how much you think it should understand you IT ISNT GOING TO. PERIOD. It is up to you to state exactly what you want with exactly the right syntax EVERY SINGLE SOLITARY TIME WITHOUT EXCEPTION. Comments # comments follow a pound sign Help Variables x = 9+1j name = "john" age = 36 age_string = str(age) length_of_age_string = len(age_string) todays_date = "1 mar 2018" it_is_january = "jan" in todays_date # it_is_january = true it_is_february = "feb" in todays_date # it_is_february = false OrderedList = item", 2, "third item" # OrderedList0 = "first item" Print print(name, "is", age, "years old") # default separater is a single space. default end is new line print("this is printed on a new line") print("But we can change the def", "ault separator and default end", sep="", end="") print("This is not printed on a new line") print("a\tbx\tc\n12345678901234567890") # \t is tab and \n is new line. produces: john is 36 years old this is printed on a new line But we can change the default separator and default endThis is not printed on a new line a bx c 12345678901234567890 Functions def convert_to_celcius(fahrenheit): ::"""Converts fahrenheit to celcius""" # documentation that is printed out when we use help("convert_to_celcius") ::return (fahrenheit-32)*5/9 print (convert_to_celcius(212)) Conditional statements young = age < 10 old = age > 65 middle_age = (45 < age < 65) if young: # dont forget the colon at the end ::print (name, "is young") elif old: ::print (name, "is old") elif middle_age: ::print (name, "is middle age") else: ::print (name, "is neither young nor old nor middle age") Input your_input = input("please enter your input: ") print ("you entered", your_input) Importing modules After importing a module you can use help("module name") to get info. For examples see ::Template:Help("time") ::Template:Help("math") import time time.sleep(15) # delay for 15 seconds import math x = math.sqrt(9) # x = sqrt(9) wont work. You must use math.sqrt import math as mt x = mt.sqrt(9) from math import sqrt x = sqrt(9) # no need for math.sqrt since sqrt has been imported into the current namespace There is nothing fancy about a module. Its just a program containing program definitions. The program is executed when it is imported unless it was previously imported. You can easily create your own modules. Calling methods is very similar to calling functions in a module. Instead of module.function(arguments) you use class.method(arguments). str.capitalize("string") # returns the string with first letter capitalized In a perfect world every module would correspond to exactly one class of object. We dont live in that world. Loops for num in range(1000, 2000, 100): # prints 1000 to 1900 in steps of 100 ::print (num) num = 0 while num < 10: # dont forget the colon ::print (num) ::num = num + 1 Text files with open("TextFile.txt", "r") as file: ::contents = file.read() with open("TextFile.txt", "r") as file: ::List_of_lines = file.readlines() with open("TextFile.txt", "w") as output_file: ::output_file.write(contents) Database Creating: import sqlite3 con = sqlite3.connect('test.db') cur = con.cursor() cur.execute('CREATE TABLE test(name TEXT, age INTEGER)') cur.execute('INSERT INTO test VALUES("john", 33)') cur.execute('INSERT INTO test VALUES("Alice", 21)') x=18 cur.execute('INSERT INTO test VALUES(?, ?)', x) con.commit() # changes are not committed until this command cur.execute('SELECT * FROM test WHERE age > 5 ORDER BY name') whole_table = cur.fetchall() print (whole_table) con.close() Updating" import sqlite3 con = sqlite3.connect('test.db') cur = con.cursor() cur.execute('DELETE FROM test WHERE name = "Mary"') cur.execute('UPDATE test SET age = 99 WHERE name = "Mary"') # if there is more than one Mary then they both get updated cur.execute('SELECT * FROM test WHERE age > 5 ORDER BY name') whole_table = cur.fetchall() print (whole_table) con.close() Internet The following code prints each line of the source code of the webpage. import urllib.request with urllib.request.urlopen("url address") as webpage: ::for line in webpage: ::::line = line.strip() ::::line = line.decode('utf-8') ::::print(line) Numpy Arrays import numpy as np x = np.array([1,2,3,4,5,6]) print (x) produces: [2 3 5 6] Math operations + and * operate elementwise and create a new matrix. To modify an existing array use += and *= For matrix multiplication use @ Scipy tutorial To import two subpackages you would use: from scipy import subpackage1, subpackage2 Matplotlib import matplotlib as mpl import matplotlib.pyplot as plt Copyright